﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;

namespace Gmantis.Controls
{
    public class TabControlAutomationPeer : ItemsControlAutomationPeer, ISelectionProvider
    {
        // Methods
        public TabControlAutomationPeer(TabControl owner)
            : base(owner)
        {
        }

        private static ItemAutomationPeer CreateItemAutomationPeer(object item)
        {
            return new TabItemAutomationPeer(item);
        }

        protected override AutomationControlType GetAutomationControlTypeCore()
        {
            return AutomationControlType.Tab;
        }

        protected override string GetClassNameCore()
        {
            return base.Owner.GetType().Name;
        }

        protected override Point GetClickablePointCore()
        {
            return new Point(double.NaN, double.NaN);
        }

        public override object GetPattern(PatternInterface patternInterface)
        {
            if (patternInterface == PatternInterface.Selection)
            {
                return this;
            }
            return base.GetPattern(patternInterface);
        }

        protected override bool IsKeyboardFocusableCore()
        {
            TabControl owner = base.Owner as TabControl;
            return (owner.IsEnabled && owner.IsTabStop);
        }

        IRawElementProviderSimple[] ISelectionProvider.GetSelection()
        {
            TabControl owner = base.Owner as TabControl;
            if (owner.SelectedItem == null)
            {
                return null;
            }
            List<IRawElementProviderSimple> list = new List<IRawElementProviderSimple>(1);
            ItemAutomationPeer peer = CreateItemAutomationPeer(owner.SelectedItem);
            if (peer != null)
            {
                list.Add(base.ProviderFromPeer(peer));
            }
            return list.ToArray();
        }

        // Properties
        bool ISelectionProvider.CanSelectMultiple
        {
            get
            {
                return false;
            }
        }

        bool ISelectionProvider.IsSelectionRequired
        {
            get
            {
                return true;
            }
        }
    }
}
